package com.syezon.wifikey.news.adapter;

import android.content.Context;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.hodanet.appadvplatclient.ad.AdInfo;
import com.syezon.wifikey.R;
import com.syezon.wifikey.imageloader.ImageLoader;
import com.syezon.wifikey.news.info.NewsInfo;
import com.syezon.wifikey.news.info.NewsItemInfo;
import com.syezon.wifikey.utils.ScreenUtil;
import com.syezon.wifikey.web.WebHelper;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 *
 */
public class SurfingHotNewsAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private Context mContext;
    private List<NewsItemInfo> mData = new ArrayList<>();
    private TopAdListAdapter mTopAdListAdapter;
    private int screenWidth;
    private List<AdInfo> mCurrentTopAdList;

    public SurfingHotNewsAdapter(Context context) {
        this.mContext = context;
        mTopAdListAdapter = new TopAdListAdapter(context);
        screenWidth = ScreenUtil.getScreenWidth(mContext);
    }

    public void setData(List<NewsItemInfo> data) {
        if (data != null && data.size() > 0) {
            mData.clear();
            mData.addAll(data);
            if (mCurrentTopAdList != null) {
                setTopAdInfo(mCurrentTopAdList);
            }
            notifyDataSetChanged();
        }
    }

    public void addData(List<NewsItemInfo> data) {
        if (data != null && data.size() > 0) {
            mData.addAll(data);
            notifyDataSetChanged();
        }
    }

    public void setTopAdInfo(List<AdInfo> adInfo) {
        if (adInfo != null && adInfo.size() > 0) {
            Iterator<NewsItemInfo> iterator = mData.iterator();
            while (iterator.hasNext()) {
                NewsItemInfo next = iterator.next();
                if (next.getType() == NewsItemInfo.TYPE_TOP_AD_LIST) {
                    iterator.remove();
                }
            }
            NewsItemInfo newsItemInfo = new NewsItemInfo();
            newsItemInfo.setType(NewsItemInfo.TYPE_TOP_AD_LIST);
            newsItemInfo.setTopAdList(adInfo);
            mData.add(0, newsItemInfo);
            mCurrentTopAdList = adInfo;
            notifyDataSetChanged();
        }
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        switch (viewType) {
            case NewsItemInfo.TYPE_NEWS_BIG_PIC:
                return new BigPicItemViewHolder(LayoutInflater.from(mContext).inflate(R.layout.item_news_bigpic, parent, false));
            case NewsItemInfo.TYPE_NEWS_SINGLE_PIC:
                return new PicTextItemViewHolder(LayoutInflater.from(mContext).inflate(R.layout.item_news_text_pic, parent, false));
            case NewsItemInfo.TYPE_NEWS_SIMPLE_TEXT:
                return new SimpleTextItemViewHolder(LayoutInflater.from(mContext).inflate(R.layout.item_news_simple_text, parent, false));
            case NewsItemInfo.TYPE_NEWS_PICS:
                return new SmallPicItemViewHolder(LayoutInflater.from(mContext).inflate(R.layout.item_hot_news, parent, false));
            case NewsItemInfo.TYPE_TOP_AD_LIST:
                return new TopAdItemViewHolder(LayoutInflater.from(mContext).inflate(R.layout.item_news_top_ad, parent, false));
            default:
                break;
        }
        return new DefaultItemViewHolder(new View(mContext));

    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (holder != null) {
            final NewsItemInfo newsItemInfo = mData.get(position);
            NewsInfo newsInfo = newsItemInfo.getNewsInfo();
            List<AdInfo> topAdList = newsItemInfo.getTopAdList();
            if (holder instanceof BigPicItemViewHolder && newsInfo != null) {
                BigPicItemViewHolder viewHolder = (BigPicItemViewHolder) holder;
                onBindBigViewHolder(viewHolder, newsInfo);
            }
            if (holder instanceof SmallPicItemViewHolder && newsInfo != null) {
                SmallPicItemViewHolder viewHolder = (SmallPicItemViewHolder) holder;
                onBindSmallPicItemViewHolder(viewHolder, newsInfo);
            }
            if (holder instanceof SimpleTextItemViewHolder && newsInfo != null) {
                SimpleTextItemViewHolder viewHolder = (SimpleTextItemViewHolder) holder;
                onBindVSimpleTextItemViewHolder(viewHolder, newsInfo);
            }
            if (holder instanceof PicTextItemViewHolder && newsInfo != null) {
                PicTextItemViewHolder viewHolder = (PicTextItemViewHolder) holder;
                onBindPicTextViewHolder(viewHolder, newsInfo);
            }
            if (holder instanceof TopAdItemViewHolder && topAdList != null) {
                TopAdItemViewHolder viewHolder = (TopAdItemViewHolder) holder;
                onBindTopAdViewHolder(viewHolder, topAdList);
            }
        }
    }

    private void onBindTopAdViewHolder(TopAdItemViewHolder viewHolder, List<AdInfo> topAdList) {
        mTopAdListAdapter.setData(topAdList);
    }

    private void onBindPicTextViewHolder(PicTextItemViewHolder viewHolder, final NewsInfo newsInfo) {
        viewHolder.tvNewsContent.setText(newsInfo.name);
        ImageLoader.getInstance().displayImage(mContext, newsInfo.picUrl, viewHolder.imgPic, R.drawable.ic_news_default);
        if (newsInfo.type == 0) {
            viewHolder.tvTuiGuang.setVisibility(View.VISIBLE);
        } else {
            viewHolder.tvTuiGuang.setVisibility(View.GONE);
        }
        if (newsInfo.author.contains("众联广告")) {
            viewHolder.tvTuiGuang.setVisibility(View.VISIBLE);
            viewHolder.tvAuthor.setVisibility(View.GONE);
        } else {
            viewHolder.tvTuiGuang.setVisibility(View.GONE);
            viewHolder.tvAuthor.setText(newsInfo.author);
            viewHolder.tvAuthor.setVisibility(View.VISIBLE);
        }
        viewHolder.tvDate.setText(newsInfo.date);
        viewHolder.tvDate.setVisibility(View.VISIBLE);
        viewHolder.contentView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                goToWeb(mContext, newsInfo);
            }
        });
    }

    private void onBindVSimpleTextItemViewHolder(SimpleTextItemViewHolder viewHolder, final NewsInfo newsInfo) {
        viewHolder.tvNewsContent.setText(newsInfo.name);
        viewHolder.tvNewsDate.setText(newsInfo.date);
        viewHolder.tvNewsSource.setText(newsInfo.author);
        viewHolder.contentView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                goToWeb(mContext, newsInfo);
            }
        });
    }

    private void onBindSmallPicItemViewHolder(SmallPicItemViewHolder viewHolder, final NewsInfo newsInfo) {
        viewHolder.tvNewsContent.setText(newsInfo.name);
        if (newsInfo.imgs != null) {
            String[] imgs = newsInfo.imgs.split("\\|");
            if (imgs != null && imgs.length > 2) {
                ImageLoader.getInstance().displayImage(mContext, imgs[0], viewHolder.imgLeft, R.drawable.ic_news_default);
                ImageLoader.getInstance().displayImage(mContext, imgs[1], viewHolder.imgCenter, R.drawable.ic_news_default);
                ImageLoader.getInstance().displayImage(mContext, imgs[2], viewHolder.imgRight, R.drawable.ic_news_default);
            }
        }
        if (newsInfo.type == 0) {
            viewHolder.tvNewsTuiGuang.setVisibility(View.VISIBLE);
        } else {
            viewHolder.tvNewsTuiGuang.setVisibility(View.GONE);
        }
        if (newsInfo.author.contains("众联广告")) {
            viewHolder.tvNewsTuiGuang.setVisibility(View.VISIBLE);
            viewHolder.tvAuthor.setVisibility(View.GONE);
        } else {
            viewHolder.tvNewsTuiGuang.setVisibility(View.GONE);
            viewHolder.tvAuthor.setVisibility(View.VISIBLE);
        }
        viewHolder.tvAuthor.setText(newsInfo.author);
        viewHolder.tvDate.setVisibility(View.VISIBLE);
        viewHolder.tvDate.setText(newsInfo.date);
        viewHolder.contentView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                goToWeb(mContext, newsInfo);
            }
        });
    }

    private void onBindBigViewHolder(BigPicItemViewHolder viewHolder, final NewsInfo newsInfo) {
        viewHolder.tvNewsContent.setText(newsInfo.name);
        if (newsInfo.imgs != null) {
            String[] imgs = newsInfo.imgs.split("\\|");
            if (imgs != null && imgs.length > 0) {
                ImageLoader.getInstance().displayImage(mContext, imgs[0], viewHolder.imgBigPic, R.drawable.ic_news_default);
            }
        }
        if (newsInfo.type == 0) {
            viewHolder.tvNewsTuiGuang.setVisibility(View.VISIBLE);
            viewHolder.tvAuthor.setVisibility(View.GONE);
        } else {
            viewHolder.tvNewsTuiGuang.setVisibility(View.GONE);
            viewHolder.tvAuthor.setVisibility(View.VISIBLE);
            viewHolder.tvAuthor.setText(newsInfo.author);
        }
        viewHolder.contentView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                goToWeb(mContext, newsInfo);
            }
        });
    }

    @Override
    public int getItemCount() {
        return mData.size();
    }

    @Override
    public int getItemViewType(int position) {
        return mData.get(position).getType();
    }

    private void goToWeb(Context context, final NewsInfo newsInfo) {
        if (newsInfo != null) {
            if (newsInfo.type == 0) {
                WebHelper.showAd(context, newsInfo.clickUrl, newsInfo.name);
            } else {
                WebHelper.showAd(context, newsInfo.clickUrl, newsInfo.name);
            }
        }
    }

    class BigPicItemViewHolder extends RecyclerView.ViewHolder {
        View contentView;
        TextView tvNewsContent;
        ImageView imgBigPic;
        TextView tvNewsTuiGuang;
        TextView tvAuthor;

        public BigPicItemViewHolder(View itemView) {
            super(itemView);
            contentView = itemView;
            tvNewsContent = (TextView) itemView.findViewById(R.id.item_news_content);
            imgBigPic = (ImageView) itemView.findViewById(R.id.item_news_img);
            ScreenUtil.adapterScreen(imgBigPic, screenWidth, (float) 0.42);
            tvNewsTuiGuang = (TextView) itemView.findViewById(R.id.item_news_tuiguang);
            tvAuthor = (TextView) itemView.findViewById(R.id.item_news_source);
        }
    }

    class SmallPicItemViewHolder extends RecyclerView.ViewHolder {
        View contentView;
        TextView tvNewsContent;
        ImageView imgLeft, imgCenter, imgRight;
        TextView tvNewsTuiGuang;
        TextView tvAuthor;
        TextView tvDate;

        public SmallPicItemViewHolder(View itemView) {
            super(itemView);
            contentView = itemView;
            int width = (screenWidth - ScreenUtil.dipTopx(mContext, 40)) / 3;
            tvNewsContent = (TextView) itemView.findViewById(R.id.item_news_content);
            imgLeft = (ImageView) itemView.findViewById(R.id.news_image_left);
            imgCenter = (ImageView) itemView.findViewById(R.id.news_image_center);
            imgRight = (ImageView) itemView.findViewById(R.id.news_image_right);
            tvNewsTuiGuang = (TextView) itemView.findViewById(R.id.item_news_tuiguang);
            tvAuthor = (TextView) itemView.findViewById(R.id.item_news_source);
            tvDate = (TextView) itemView.findViewById(R.id.item_news_date);
            ScreenUtil.adapterScreen(imgLeft, width, (float) 0.75);
            ScreenUtil.adapterScreen(imgCenter, width, (float) 0.75);
            ScreenUtil.adapterScreen(imgRight, width, (float) 0.75);
        }
    }

    class SimpleTextItemViewHolder extends RecyclerView.ViewHolder {
        View contentView;
        TextView tvNewsContent;
        TextView tvNewsDate;
        TextView tvNewsSource;

        public SimpleTextItemViewHolder(View itemView) {
            super(itemView);
            contentView = itemView;
            tvNewsContent = (TextView) itemView.findViewById(R.id.item_news_content);
            tvNewsDate = (TextView) itemView.findViewById(R.id.item_news_date);
            tvNewsSource = (TextView) itemView.findViewById(R.id.item_news_source);
        }
    }

    class PicTextItemViewHolder extends RecyclerView.ViewHolder {
        View contentView;
        TextView tvNewsContent;
        ImageView imgPic;
        TextView tvTuiGuang;
        TextView tvAuthor;
        TextView tvDate;

        public PicTextItemViewHolder(View itemView) {
            super(itemView);
            contentView = itemView;
            tvNewsContent = (TextView) itemView.findViewById(R.id.item_news_content);
            imgPic = (ImageView) itemView.findViewById(R.id.item_news_img);
            tvTuiGuang = (TextView) itemView.findViewById(R.id.item_news_tuiguang);
            tvAuthor = (TextView) itemView.findViewById(R.id.item_news_source);
            tvDate = (TextView) itemView.findViewById(R.id.item_news_date);
            int width = (screenWidth - ScreenUtil.dipTopx(mContext, 40)) / 3;
            ScreenUtil.adapterScreen(imgPic, width, (float) 0.75);
        }
    }

    class TopAdItemViewHolder extends RecyclerView.ViewHolder {

        RecyclerView rvAdList;

        public TopAdItemViewHolder(View itemView) {
            super(itemView);
            rvAdList = (RecyclerView) itemView.findViewById(R.id.rv_ad_list);
            rvAdList.setLayoutManager(new GridLayoutManager(mContext, 4));
            rvAdList.setAdapter(mTopAdListAdapter);
        }
    }

    class DefaultItemViewHolder extends RecyclerView.ViewHolder {
        public DefaultItemViewHolder(View itemView) {
            super(itemView);
        }
    }
}
